/**
 * Copyright (c) 2005, Corey Goldberg.
 * @author Corey Goldberg
 * @version 2005
 * StopWatch.java is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
*/
public class StopWatch 
{
  /**
  * Conversion between seconds to milliseconds.
  */
  private static final int MS_PER_SEC = 1000;

  /**
   * The start of the timer cycle.
   */
  private long my_start_time;
    
  /**
   * The end of the timer cycle.
   */
  private long my_stop_time;
  
  /**
   * Indicates if the timer is running or not.
   */
  private boolean my_is_running;

  /**
   * Starts the stopwatch timer.  
   */
  public void start() 
  {
    this.my_start_time = System.currentTimeMillis();
    this.my_is_running = true;
  }

  /** 
   * Stops the stopwatch timer.  
   */
  public void stop() 
  {
    this.my_stop_time = System.currentTimeMillis();
    this.my_is_running = false;
  }

    
    /**
     * Gets the elaspsed time in milliseconds.
     * @return The elapsed time in ms.
     */
  public long getElapsedTime() 
  {
    long elapsed;
    if (my_is_running) 
    {
      elapsed = System.currentTimeMillis() - my_start_time;
    }
    else 
    {
      elapsed = my_stop_time - my_start_time;
    }
    
    return elapsed;
  }
    
    
    /**
     * Gets the elapsed time in seconds.
     * @return Elapsed time in seconds.
     */
  public long getElapsedTimeSecs() 
  {
    long elapsed;
    if (my_is_running) 
    {
      elapsed = (System.currentTimeMillis() - my_start_time) / MS_PER_SEC;
    }
    else
    {
      elapsed = (my_stop_time - my_start_time) / MS_PER_SEC;
    }
      
    return elapsed;
  }

}
